
import utils

config = utils.get_config();

#read file
dictMap = {};
d = open(config.get('rmls', 'dictionary'), 'r');
index = 0;
for line in d:
    dictMap[index] = line.strip('\n').split('\t')[0].decode('utf-8')
    index += 1;
d.close();
MAX = len(dictMap);
print MAX;


def computeDis(Lx, i, j):
    x = Lx[i] - Lx[j];
    
    return utils.sparseFroNorm(x);

def write_csv(f, row, index):
    row = row.todense().getA1();
    f.write('%s,%s\n' % (dictMap[index].encode('utf-8'), ','.join(map(str, row)))); 

def bad(A):
    for a in A:
        if a !=0 and a < 0.5:
            return True;
    return False;

if __name__ == '__main__':
    Lx_1 = utils.load_sparse_csr('Lx_rp100_week.txt.npz');
    Lx_2  = utils.load_sparse_csr('Lx_rp100_c_week.txt.npz')
    f = open('plot_S', 'r');
    csv1 = open('1.csv', 'w')
    csv2 = open('2.csv', 'w');
    plot_dict = {};
    for line in f:
        li = line.strip().split(',');
        print len(li)
        i = int(li[1]);
        j = int(li[2]);
        if plot_dict.has_key(i):
            pass;
        else:
            plot_dict[i] = dictMap[i];
        if plot_dict.has_key(j):
            pass;
        else:
            plot_dict[j] = dictMap[j];

    R = set(plot_dict.keys());
    
    #for i in plot_dict.keys():
    #    for j in R:
    #        if computeDis(Lx_1, i, j) < 0.01:
    #            R.remove(j);
    
    a = ','+','.join(map(str, range(len(R)))) + '\n'

    csv1.write(a);
    csv2.write(a);
    print len(R);
    for i in R:
        dis_1 = [];
        dis_2 = [];
        for j in R:
            dis_1.append(computeDis(Lx_1, i, j));
            dis_2.append(computeDis(Lx_2, i, j));
        csv1.write('%s,%s\n' % (str(i)+dictMap[i].encode('utf-8'), ','.join(map(str, dis_1))));
        csv2.write('%s,%s\n' % (str(i)+dictMap[i].encode('utf-8'), ','.join(map(str, dis_2))));

    csv1.close();
    csv2.close();
